<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once('DataProcessor.php');

class ProcessRelatedItems extends DataProcessor{
	function ProcessRelatedItems( &$p ){
		parent::DataProcessor($p);
	}
	
	/* removes relational entries */
	function removeRelatedItems($id, $fields){
		if( $id ){
			/* remove everything from projects_rel_projects where project_id = $id */
			foreach( $fields as $key => $value){
				$table = $value['table'];
				$id_fields = $value['id_fields'];
				if( $this->ci->db->table_exists($table) ){
					$this->ci->db->query("DELETE FROM $table WHERE {$id_fields[0]} = $id");
				}
			}
		}
	}
	
	function saveRelatedItems($id, $fields){
		$this->removeRelatedItems($id, $fields);
		foreach( $fields as $key => $value){
			$table = $value['table'];
			$id_fields = $value['id_fields'];
			if( $this->ci->db->table_exists($table)){
				/* grab the selected items from post[$key] and insert into projects_rel_project */
				$items = $this->ci->input->post($key);
				if( $items && is_array($items)){
					$items = array_map( array($this->ci->db, 'escape'), array_filter( $items, 'is_numeric'));
					if( count($items) ){
						$values = "( $id, " . implode( "), ($id, ", $items) . ')';
						$this->ci->db->query("INSERT INTO $table ({$id_fields[0]}, {$id_fields[1]}) VALUES $values");
					}
				}
			}else{
				show_error("ProcessRelatedItems::saveItem - table $table does not exist.");
			}
		}
	}
	
	function addItemOptions($id, $fields){
		$params = array();
		foreach( $fields as $key => $value){
			$table = $value['table'];
			$item_table = $value['item_table'];
			$order_by = null;
			$display_field = $value['display_field'];
			if( isset($value['order_by'])){
				$order_by = $value['order_by'];
			}
			$where = null;
			if( isset($value['where'])){
				$where = $value['where'];
			}
			$id_fields = $value['id_fields'];
			$item_id_field = isset($value['item_id_field'])?$value['item_id_field']:$id_fields[1];
			/* this should really load a model so that we can manage archived items and live items properly */
			if( $this->ci->db->table_exists($table) && $this->ci->db->table_exists($item_table)){
				$options = array();
				$selected = array();
				
				if($where){
					$this->ci->db->where($where);
				}
				/* options are all projects except this one */
				if( $id && isset($value['exclude_same_id']) && $value['exclude_same_id'] == true){
					/* don't get item with this id */
					$this->ci->db->where($item_id_field . '<>' . $id);
				}
				if( $this->ci->db->field_exists('is_archive', $item_table)){
					$this->ci->db->where('is_archive = 0');
				}
				if( $this->ci->db->field_exists('is_live', $item_table)){
					$this->ci->db->where('is_live = 1');
				}
				
				if($order_by){
					$this->ci->db->order_by($order_by);
				}
				$query = $this->ci->db->get($item_table);
				//echo "query: " . $this->ci->db->last_query() . "<br />";
				$query = $query->result_array();
				foreach( $query as $row){
					//$options[$row[$id_fields[0]]] = $row[$display_field];
					$options[$row[$item_id_field]] = $row[$display_field];
				}
				
				if( $this->ci->input->post($key) && is_array($this->ci->input->post($key))){
					$selected = $this->ci->input->post($key);
					//echo "getting selected from post($key)";
					//echo_html_object($selected);
				}else if($id){
					$query = $this->ci->db->query("SELECT {$id_fields[1]} FROM $table WHERE {$id_fields[0]} = $id");
					if( $query && $query->num_rows()){
						foreach($query->result_array() as $row){
							$selected[] = $row[$id_fields[1]];
						}
					}
				}
				
				$params[$key] = array($options, $selected);
				unset($options);
				unset($selected);
			}else{
				show_error("ProcessRelatedProjects::addItemOptionParameters - table $key does not exist");
			}
		}
		//echo "params:";
		//echo_html_object($params);
		$this->parent_model->addEditParameters($params);
	}
	
}

?>